<?php

namespace App\Api\Controller;


use App\Answer;
use App\Api\Transformers\MasterAllTransformer;
use App\Api\Transformers\MasterTransformer;
use App\Api\Transformers\MasterVideoTransformer;
use App\Master;
use App\MasterVideo;
use Illuminate\Http\Request;


/**
 * Class MasterController
 * @package App\Api\Controller
 */
class MasterController extends BaseController
{
    /**
     * return all master
     *
     * @return \Dingo\Api\Http\Response
     */
    public function index()
    {
        $masters = Master::all();

        return $this->response->collection($masters, new MasterTransformer());
    }

    /**
     * return master all info
     *
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function show(Request $request)
    {
        $master_id = $request->get('masterid');
        $master = Master::where('id',$master_id)->get();

        if ($master->isEmpty())
        {
            return $this->response->errorNotFound('没有找到此大师');
        }

        return $this->response->collection($master, new MasterAllTransformer());
    }

    /**
     * return video info
     *
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function video(Request $request)
    {
        $video_id = $request->get('videoid');
        $mvideo = MasterVideo::where('id',$video_id)->get();

        if($mvideo->isEmpty())
        {
            return $this->response->errorNotFound('没有找到此视频');
        }

        return $this->response->item($mvideo, new MasterVideoTransformer());
    }

    public function store(Request $request)
    {
        $newAnswer = [
            'answer_content' => $request->get('content'),
            'master_name' => $request->get('mastername'),
            'question_id' => $request->get('questionid'),
            'master_id' => $request->get('masterid')
        ];

        $master = Master::where('id',$request->get('masterid'))->get();

        if($master->isEmpty())
        {
            return $this->response->errorNotFound('不是大师,不能回答哦');
        }

        Answer::create($newAnswer);

        return response([
            'success' => 1,
        ]);

    }
}